import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * 下面的菜单是所有用户都能够看见的
 */
export const constantRoutes = [{
  path: '/login',
  component: () => import('@/views/login/index'),
  hidden: true
},
{
  path: '/404',
  component: () => import('@/views/error-page/404'),
  hidden: true
},
{
  path: '/401',
  component: () => import('@/views/error-page/401'),
  hidden: true
},
{
  path: '/',
  component: Layout,
  redirect: '/dashboard',
  children: [{
    path: 'dashboard',
    component: () => import('@/views/dashboard/index'),
    name: 'Dashboard',
    meta: {
      title: '首页',
      icon: 'dashboard',
      affix: true
    }
  }]
}
]

/**
 * asyncRoutes
 * 下面的菜单会根据用户权限展示
 * 权限: 1是系统管理员, 2是区域管理员, 3是技术部管理员, 4是巡检部管理员, 6是巡检部班长, 7是巡检员
 */
export const asyncRoutes = [
  {
    path: '/roadAudit',
    component: Layout,
    redirect: '/roadAudit/index',
    name: 'roadAudit',
    children: [{
      path: 'index',
      component: () => import('@/views/roadAudit/RoadAudit'),
      name: 'index',
      meta: {
        title: '道路审核',
        icon: 'lock',
        roles: ['1']
      }
    }]
  }, {
    path: '/roadManager',
    component: Layout,
    redirect: 'noRedirect',
    name: 'roadManager',
    meta: {
      icon: 'list',
      roles: ['1', '2']
    },
    children: [{
      path: 'index',
      component: () => import('@/views/roadAudit/RoadManage'),
      name: 'roadManagerIndex',
      meta: {
        title: '道路管理',
        noCache: true,
        roles: ['1', '2']
      }
    }]
  }, {
    path: '/roadEvaluation',
    component: Layout,
    redirect: 'noRedirect',
    name: 'roadEvaluation',
    meta: {
      icon: 'education',
      roles: ['1']
    },
    children: [{
      path: 'index',
      component: () => import('@/views/evaluation/EvaluationResult'),
      name: 'roadEvaluationIndex',
      meta: {
        title: '道路年报表',
        noCache: true,
        roles: ['1']
      }
    }]
  },
  {
    path: '/damageNoticeManager',
    component: Layout,
    redirect: 'noRedirect',
    name: 'damageNoticeManager',
    meta: {
      icon: 'lock',
      roles: ['2']
    },
    children: [{
      path: 'index',
      component: () => import('@/views/daily-inspection/DamageNoticeManager'),
      name: 'damageNoticeManagerIndex',
      meta: {
        title: '整修审核',
        noCache: true,
        roles: ['2']
      }
    }]
  },
  {
    path: '/plan',
    component: Layout,
    redirect: 'noRedirect',
    name: 'plan',
    meta: {
      title: '巡检计划',
      icon: 'table',
      roles: ['1', '2', '4']
    },
    children: [{
      path: 'dailyInspectionPlan',
      component: () => import('@/views/daily-inspection/AdminInspectionPlanTable'),
      name: 'dailyInspectionPlan',
      meta: {
        title: '日常巡检计划',
        noCache: true,
        roles: ['1', '2', '4', '6', '7']
      }
    },
    {
      path: 'regularInspectionPlan',
      component: () => import('@/views/regular-inspection/RegularInspectionTable'),
      name: 'regularInspectionPlan',
      meta: {
        title: '定期巡检计划',
        noCache: true,
        roles: ['1', '2', '4', '6', '7']
      }
    }
    ]
  }, {
    path: '/dailyInspectionEnroll',
    component: Layout,
    redirect: 'dailyInspectionEnroll/index',
    name: 'dailyInspectionEnroll',
    meta: {
      icon: 'form',
      roles: ['6', '7']
    },
    children: [{
      path: 'index',
      component: () => import('@/views/daily-inspection/DailyInspectionEnroll'),
      name: 'dailyInspectionEnrollIndex',
      meta: {
        title: '日常巡检登记',
        noCache: true,
        roles: ['6', '7']
      }
    }]
  }, {
    path: '/regularInspectionEnroll',
    component: Layout,
    redirect: 'regularInspectionEnroll/index',
    name: 'regularInspectionEnroll',
    meta: {
      icon: 'form',
      roles: ['6', '7']
    },
    children: [{
      path: 'index',
      component: () => import('@/views/regular-inspection/PlanTable'),
      name: 'regularInspectionEnrollIndex',
      meta: {
        title: '定期巡检登记',
        noCache: true,
        roles: ['6', '7']
      }
    }]
  },
  {
    path: '/renovateEnroll',
    component: Layout,
    children: [{
      path: 'index',
      component: () => import('@/views/daily-inspection/RenovateEnroll'),
      name: 'renovateEnroll',
      meta: {
        title: '整修登记',
        icon: 'skill',
        roles: ['5', '8']
      }
    }]
  },

  {
    path: 'external-link',
    component: Layout,
    children: [{
      path: 'https://gitee.com/VioletFreesia/RoadMaintenanceSystem',
      meta: {
        title: '项目地址',
        icon: 'link'
      }
    }]
  },

  // 404 page must be placed at the end !!!
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

const createRouter = () => new Router({
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
